
<!DOCTYPE html>
<html>
<head>[#include "../manage/common/head.html" /]
</head>

<body class="gray-bg">
	<div class="wrapper wrapper-content">
		<div class="row">
			<div class="col-sm-12">
				<div class="ibox float-e-margins">
					<div class="ibox-title">
						<h5>上架宝贝</h5>
					</div>
					<div class="ibox-content">
						<table
							class="table table-striped table-bordered table-hover dataTables">
							<thead>
								<tr>
									<th class="text-center">商品名称</th>
									<th class="text-center">商品类别</th>
									<th class="text-center">优惠信息</th>
									<!-- <th class="text-center">商品成本价</th> -->
									<th class="text-center">商品销售价格</th>
									<th class="text-center">商品库存</th>
									<th class="text-center">警告库存</th>
									<th class="text-center">是否上架</th>
									<th class="text-center">操作项</th>
								</tr>
							</thead>
							<tbody>
							</tbody>
						</table>

					</div>
				</div>
			</div>
		</div>
	</div>
	<div class="modal fade" id="windowModal" tabindex="-1" role="dialog"
		aria-hidden="true">
		<div class="modal-dialog modal-lg">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-hidden="true">&times;</button>
					<h4 class="modal-title">新建</h4>
				</div>

				<div class="modal-body">
					<form class="form-horizontal" id="myForm">
						<div class="form-group">
							<label class="col-sm-2 control-label">优惠信息</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="favorable">
							</div>
						</div>
						<!-- <div class="form-group">
							<label class="col-sm-2 control-label">商品成本价</label>
							<div class="col-sm-10">
								<input type="number" class="form-control" min="0" step="0.01" id="costPrice" />
							</div>
						</div> -->
						<div class="form-group">
							<label class="col-sm-2 control-label">商品销售价格</label>
							<div class="col-sm-10">
								<input type="number" class="form-control" min="0" step="0.01" id="shopPrice" />
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-2 control-label">商品库存</label>
							<div class="col-sm-10">
								<input type="number" class="form-control" min="0" id="inventory" />
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-2 control-label">警告库存</label>
							<div class="col-sm-10">
								<input type="number" class="form-control" min="0" id="warning" />
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-2 control-label">是否上架</label>
							<div class="col-sm-10">
								<select id="isShelves" class="selectpicker show-tick form-control"
									data-live-search="false">
									<option value=""></option>
									<option value="0">下架</option>
									<option value="1">上架</option>
								</select>
							</div>
						</div>
					</form>
				</div>
				<div class="modal-footer clearfix">
					<div class="form-group col-md-8 col-sm-offset-2 text-center">
						<button type="button" class="btn btn-primary" id="save">保存</button>
					</div>
				</div>
			</div>
		</div>
	</div>
	[#include "../manage/common/js.html" /]
	<script>
	$(function() {
		dt = $('.dataTables').dataTable(
			{
				"ordering" : false,
				"searching" : false,
				"info" : true,
				"bLengthChange" : false,
				"pagingType" : "full_numbers",
				"language" : {
					"lengthMenu" : "每页 _MENU_ 条记录",
					"zeroRecords" : "没有找到记录",
					"info" : "第 _PAGE_ 页 ( 总共 _PAGES_ 页 )",
					"infoEmpty" : "无记录",
					"infoFiltered" : "(从 _MAX_ 条记录过滤)",
					"sEmptyTable" : "表中数据为空",
					"sLoadingRecords" : "载入中...",
					"oPaginate" : {
						"sFirst" : "首页",
						"sPrevious" : "上一页",
						"sNext" : "下一页",
						"sLast" : "末页"
					}
				},
				"serverSide" : true,
				"sAjaxSource" : "${base}/manage/goods/selectInfoList",
				"columns" : [
						{
							"data" : "name",
							"sWidth" : "20%"
						},
						{
							"data" : "categoryName",
							"sWidth" : "10%"
						},
						{
							"data" : "favorable",
							"sWidth" : "20%"
						}
						/* ,
						{
							"data" : "costPrice",
							"sWidth" : "10%"
						} */
						,
						{
							"data" : "shopPrice",
							"sWidth" : "10%"
						},
						{
							"data" : "inventory",
							"sWidth" : "10%"
						},
						{
							"data" : "warning",
							"sWidth" : "10%"
						},
						{
							"data" : "isShelves",
							"sWidth" : "10%",
							"render" : function(data) {
								if (data == 0) {
									return '下架'
								} else if (data == 1) {
									return '上架'
								} else
									return "";
							}
						},
						{
							"data" : {
								"id" : "id"
							},
							"sWidth" : "10%",
							"render" : function(data) {
								return '<div class="text-center"><button class="btn btn-info btn-sm" type="button" data-title="编辑" data-toggle="modal" data-target="#windowModal" data-id="'
										+ data.id
										+ '" onclick="edit('
										+ data.id
										+ ')">编辑</button>';
							}
						} ],
				"fnServerData" : function(sSource, aoData,
						fnCallback) {
					$.ajax({
						"dataType" : 'json',
						"type" : "POST",
						"url" : sSource,
						"data" : aoData,
						"success" : fnCallback
					});
				}
			});
		})

		function search() {
			dt.fnFilter();
		}

		function restForm() {
			$("#favorable").val("");
			/* $("#costPrice").val(0.00); */
			$("#shopPrice").val(0.00);
			$("#inventory").val(0);
			$("#warning").val(0);
			$("#isShelves").val("");
		}

		$("#windowModal").on("hidden.bs.modal", function() {
			restForm();
		});

		var oid = 0;
		$("#save").click(function() 
				{
					var favorable = $("#favorable").val().trim();
					/* if (favorable == "") {
						swal("警告", "请填写优惠信息！", "warning");
						return;
					} */
					/* var costPrice = $("#costPrice").val().trim();
					if (costPrice == "") {
						swal("警告", "请填写商品成本价！", "warning");
						return;
					} */
					var shopPrice = $("#shopPrice").val().trim();
					if (shopPrice == "") {
						swal("警告", "请填写商品销售价格！", "warning");
						return;
					}
					var inventory = $("#inventory").val().trim();
					if (inventory == "") {
						swal("警告", "请填写商品库存！", "warning");
						return;
					}
					var warning = $("#warning").val().trim();
					if (warning == "") {
						swal("警告", "请填写警告库存！", "warning");
						return;
					}
					var isShelves = $("#isShelves").val();
					if (isShelves == "") {
						swal("警告", "请选择是否上架！", "warning");
						return;
					}
					$.post("${base}/manage/goods/updateShelves", {
						id : oid,
						favorable : favorable,
						/* costPrice : costPrice, */
						shopPrice : shopPrice,
						inventory : inventory,
						warning : warning,
						isShelves : isShelves
					}, function(r) {
						if (r.code == 0) {
							swal("成功", "保存完成！", "success");
							$('#windowModal').modal('hide');
							search();
						} else {
							swal("错误", r.msg, "error");
						}
					}, 'json')
				})

		function edit(id) {
			$.post("${base}/manage/goods/getInfo/"+id,function(r){
				
				$("#favorable").val(r.infoDate.favorable);
				/* $("#costPrice").val(r.infoDate.costPrice); */
				$("#shopPrice").val(r.infoDate.shopPrice);
				$("#inventory").val(r.infoDate.inventory);
				$("#warning").val(r.infoDate.warning);
				$("#isShelves").val(r.infoDate.isShelves);
				oid = id;
			},'json')
		}
	</script>
</body>
</html>
